package sxt.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /*解决post中文乱码  即可以正确的接收从浏览器发送过来的中文字符*/
        req.setCharacterEncoding("utf8");/*!!!*/

        /*设置resp.getWriter().print();打印输出数据的格式*/
        resp.setContentType("text/html;charset=utf-8");

        String method = req.getParameter("method");

        /*第二种判断要调用方法的方式-反射  注意:需要保证传入的参数和要调用的方法名一致*/
        Class clazz = this.getClass();/*获得当前类的类对象 注意:如果发送继承关系,this为调用者的类对象*/

        //System.out.println(clazz);

        try {/*获得方法的类对象       调用私有方法                后面两个是传入参数*/
            Method method1 = clazz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);

            method1.setAccessible(true);/*现在可以调用私有方法了*/

            method1.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        /*if ("deptSave".equals(method)){
            deptSave(req,resp);
        }else if ("deptFindAll".equals(method)){

            deptFindAll(req,resp);
        }else if ("deptFindOne".equals(method)){
            deptFindOne(req,resp);
        }else if ("deptChange".equals(method)){
            deptChange(req,resp);
        }else if ("deptRemove".equals(method)){
            deptRemove(req,resp);
        }*/
    }
}
